﻿namespace BEGDC.CC.Web.UserControls.OutCR
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Collections;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using BEGDC.CC.BL.CCFlow.CR;
	using BEGDC.CC.Entity;
	using BEGDC.CC.BL.Workflow;
	using BEGDC.CC.BL.Common;
	using BEGDC.Utility.Castle;
	using BEGDC.CC.BL.Common.Query;

	/// <summary>
	///		Summary description for CloseApp.
	/// </summary>
	public class CloseApp : BaseOutCR
	{
		#region Variables
		protected System.Web.UI.WebControls.ImageButton Btn_Submit;
		protected System.Web.UI.WebControls.ImageButton Btn_Save;
		protected System.Web.UI.WebControls.ImageButton btnQuery;
		protected System.Web.UI.WebControls.TextBox tbOperationFlag;
		protected System.Web.UI.WebControls.TextBox tbOperationContent;
		protected System.Web.UI.WebControls.TextBox tbComment;
		protected System.Web.UI.WebControls.TextBox Ctl_EZVisitID;
		protected System.Web.UI.WebControls.TextBox Ctl_LegalID;
		protected System.Web.UI.WebControls.TextBox Ctl_Comment;
		protected BEGDC.CC.Web.UserControls.OutCR.EZCtrlForCO EZCtrlForCO;
		protected BEGDC.CC.Web.Common.StampCtrl StampCtrl1;
		#endregion

		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!this.IsPostBack)
			{
				
				Session[SessionFlag.AccompanySession] = null;
				Session[SessionFlag.CustomerSession] = null;
				Session[SessionFlag.AccompanySessionEZ] = null;
				Session[SessionFlag.CustomerSessionEZ] = null;

				Session[SessionFlag.CompanyCarSession] = null;
				Session[SessionFlag.CompanyCarSessionEZ] = null;
				Session[SessionFlag.LeaseCarSession] = null;
				Session[SessionFlag.LeaseCarSessionEZ] = null;
				Session[SessionFlag.HotelSession] = null;
				Session[SessionFlag.HotelSessionEZ] = null;

				Session[SessionFlag.ContractInfo] = null;
				Session[SessionFlag.CostCenter] = null;

				base.PreInitForm();

				this.Btn_Save.Attributes["onclick"] = "javascript:OnSave();";
				this.Btn_Submit.Attributes["onclick"] = "javascript:OnSubmit();";
				this.btnQuery.Attributes.Add("onclick", "javascript:OpenQueryPane("+Request.QueryString["AppID"]+");");
				ButtonStatusSetting();

				InitForm();
				//SetStamp();
			}
		}

		#region SetStamp
//		private void SetStamp()
//		{
//			IQuery queryService = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
//			DataTable table = queryService.QueryActionLog( this.AppID );
//			StampCtrl1.SetDataSource( table );
//		}
		#endregion

		#region InitForm
		private void InitForm()
		{
			bool isReadOnly = true;
			long flowId  = this.LoadApplication();

			if(this.Application!=null)
			{
				EZCtrlForCO.Visible = true;
				EZCtrlForCO.isReadOnly = isReadOnly;

				this.Ctl_EZVisitID.Text = this.Application.EZVisitReqNo;
				this.Ctl_LegalID.Text = this.Application.LGDNo;
			}
			this.Ctl_Comment.Text = this.Application.Summary;
		}
		#endregion

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnQuery.Click += new System.Web.UI.ImageClickEventHandler(this.btnQuery_Click);
			this.Btn_Save.Click += new System.Web.UI.ImageClickEventHandler(this.Btn_Save_Click);
			this.Btn_Submit.Click += new System.Web.UI.ImageClickEventHandler(this.Btn_Submit_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		#region Save
		private void Btn_Save_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
				BEGDC.CC.Entity.Application App = CRService.GetApplication(this.AppID);

				App.EZVisitReqNo = this.Ctl_EZVisitID.Text;
				App.LGDNo = this.Ctl_LegalID.Text;
				App.Summary = this.Ctl_Comment.Text;

				CRService.SaveEZCloseApp(App);

				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');</script>");
			}
			catch(Exception exp)
			{
				//throw exp;
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');</script>");
			}
		}
		#endregion

		#region Submit
		private void Btn_Submit_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
				BEGDC.CC.Entity.Application App = CRService.GetApplication(this.AppID);

				if(this.Ctl_EZVisitID.Text!=""&&this.Ctl_LegalID.Text!="")
				{
					App.EZVisitReqNo = this.Ctl_EZVisitID.Text;
					App.LGDNo = this.Ctl_LegalID.Text;
					App.Summary = this.Ctl_Comment.Text;

					CRService.SubmitEZCloseApp(App, false, this.tbComment.Text);

					Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.close();</script>");
				}
				else
				{
					Page.RegisterClientScriptBlock("","<script>alert('Please fill in EZVisit ID, Legal ID and Knowledge sharing');</script>");
					return;
				}
			}
			catch(Exception exp)
			{
				//throw exp;
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');</script>");
			}
		}
		#endregion

		#region Query
		private void btnQuery_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				if(this.tbOperationFlag.Text == "query")
				{
					ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
					CRService.COQuery(Convert.ToInt64(Request.QueryString["FlowID"]), this.tbOperationContent.Text, Session["Comment"].ToString());
				}
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.close();</script>");
			}
			catch
			{
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');</script>");
			}
		}
		#endregion

		#region ButtonStatusSetting
		protected override void ButtonStatusSetting()
		{			
			if( this.Access == null )
				return;

			this.Btn_Save.Visible = this.Access.IsButtonVisible(COTransitionList.Save);
			this.Btn_Submit.Visible = this.Access.IsButtonVisible(COTransitionList.Close);
			this.btnQuery.Visible = this.Access.IsButtonVisible(COTransitionList.Query);
		}
		#endregion
	}
}
